package com.fhz.high5kotlin

import android.os.Bundle
import android.os.CountDownTimer
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.fhz.high5kotlin.life.MyLife
import com.fhz.high5kotlin.viewmodel.Car
import com.fhz.high5kotlin.viewmodel.MyViewModel
import com.fhz.high5kotlin.viewmodel.MyViewModelFactroy
import com.fhz.high5kotlin.widget.MyDialog

class MainActivity : AppCompatActivity() {

    private val m_textView: TextView by lazy {
        findViewById<TextView>(R.id.textView)
    }
    private val m_button: Button by lazy {
        findViewById<Button>(R.id.button)
    }

    private val TAG = "MainActivity"

    lateinit var dialog : MyDialog
    lateinit var myLife: MyLife
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

//        val viewModel = ViewModelProvider(this).get(MyViewModel::class.java)
        val viewModel = ViewModelProvider(this,MyViewModelFactroy(111)).get(MyViewModel::class.java)

        m_textView.text = viewModel.num.toString()

        viewModel.ageVal.observe(this){
            m_textView.text = it.toString()
        }

        viewModel.carVal.observe(this){
            m_textView.text = it.toString()
        }

        val car = Car("xiaopeng", 20, "xiaopeng")


        m_button.setOnClickListener {
//            m_textView.text = viewModel.num.toString()
//            viewModel.currentName.value = viewModel.currentName.value?.plus(1)

//            viewModel.updateAge(123123)
            viewModel.updateCar(car)

        }



//        dialog = MyDialog(this)
//        myLife = MyLife(dialog)
//        lifecycle.addObserver(dialog)

//        Handler().postDelayed({
//            finish()
//        },2000)
    }






}